package com.miage.itrading.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.miage.itrading.shared.entites.Cotation;

@SuppressWarnings("deprecation")
public class SearchWidget extends GenericWidget {

	private TextBox						search;
	private final GetListesServiceAsync	getListesService	= GWT
																	.create(GetListesService.class);

	public SearchWidget(ItradingAffichage context, String uid) {
		super(context, uid);
	}

	public SearchWidget(ItradingAffichage context, String uid, String separateur) {
		super(context, uid);
	}

	public void configureWidget() {
		search.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				System.out.println("client : recherche du mnémo "
						+ event.getValue());

				final AsyncCallback<Cotation> cotation = new AsyncCallback<Cotation>() {

					public void onFailure(Throwable caught) {
						Window.alert("Erreur lors de l'appel serveur "
								+ caught.getMessage());
					}

					public void onSuccess(Cotation result) {
						if (result.getMnemonique() == null) {
							Window
									.alert("impossible de trouver une cotation avec le mnémonique saisie");
						} else {
							context.afficheCoursIntraday(result);
						}
					}
				};
				getListesService.getByMnemo(event.getValue(), cotation);
			}
		});
	}

	public Widget createWidget() {
		search = new TextBox();
		search.setStyleName("searchTextBox");
		search.setWidth("70%");

		return search;
	}
}
